package com.genesis.yunnanji.utils;

import android.util.Base64;

import com.genesis.yunnanji.Constants;
import com.youth.xframe.utils.XDateUtils;
import com.youth.xframe.utils.XEncryptUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedHashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 创建日期：2018/9/29 002917:29
 * 作者：Kite
 * 描述：接口参数
 */
public class NetUtils {

    public static RequestBody initNet(JSONObject params) {
        long format = XDateUtils.string2Millis(XDateUtils.getCurrentDate());
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("client_id", Constants.ANDROID_CLIENT_ID);
            jsonObject.put("timestamp", format);
            jsonObject.put("content", params);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String s = jsonObject.toString();

        String decodeStr = Base64.encodeToString(s.getBytes(), Base64.NO_WRAP);
        String ss = decodeStr + Constants.SECRET_KEY;
//        String md5String = MD5Util.getMD5String(ss);
        String md5String = XEncryptUtils.MD5(ss);
        LinkedHashMap<String, Object> paramsMap = new LinkedHashMap<>();
        paramsMap.put("body", decodeStr);
        paramsMap.put("signature", md5String);

        JSONObject jsonObjectDecode = new JSONObject();
        try {
            jsonObjectDecode.put("body", decodeStr);
            jsonObjectDecode.put("signature", md5String);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonObjectDecode.toString());
        return body;
    }

}
